package net.gz01.wallet.common;

import java.text.DecimalFormat;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import net.gz01.util.ZOAliSms;
import net.gz01.wallet.web.config.SystemConfig;

/**
 * 钱包系统短信
 * @author zhangyu
 */
@Slf4j
public class WalletSMS {

	/** 阿里云短信参数：id */
	public static final String SMS_AccessKeyId = SystemConfig.config.get("sms_accessKeyId");
	/** 阿里云短信参数：密钥 */
	public static final String SMS_AccessSecret =  SystemConfig.config.get("sms_accessSecret");
	/** 阿里云短信参数：短信名称 */
    public static final String SMS_Name = "友利";

    /** 短信模板固定参数：客服电话 */
    public static final String SMS_Service = "0755-2759-5390";
    
    /** 短信存储到Redis中的key前缀 */
    public static final String REDIS_PREFIX = "allInfo_";
    
    
    /** 短信模板：登录发送验证码 */
    public static final String Tpl_Login = "SMS_165850173";
    /** 短信模板：设置支付密码 */
    public static final String Tpl_SetPayPassword="SMS_166095957";
    /** 短信模板：钱包充值 */
    public static final String Tpl_WalletCharge="SMS_166095979";
    /** 短信模板：钱包支出 */
    public static final String Tpl_Expend="SMS_169901978";
    
    /** 发送账户余额变动短信 */
	private static boolean sendForAmtChange(String phoneNo, String type, long amount, long balance) {
    	String amt = new DecimalFormat("#,##0.00").format(amount / 100.0);
        JSONObject param = new JSONObject();
        param.put("user", phoneNo);
		param.put("todoamtShow", type + ":￥" + amt);
		param.put("balance", "￥" + new DecimalFormat("#,##0.00").format(balance / 100.0));
        param.put("service", SMS_Service);

        ZOAliSms zoAliSms = new ZOAliSms(SMS_AccessKeyId, SMS_AccessSecret);
        JSONObject result = zoAliSms.send(phoneNo, param.toJSONString(), SMS_Name, Tpl_WalletCharge);
        if ("OK".equals(result.getString("Code"))) {
            return true;
        } else {
			log.error("发送金额变动短信失败，接收手机号：" + phoneNo + "，变动金额：" + amt + "，阿里云返回：" + result.toJSONString());
            return false;
        }
	}

    /** 发送加钱短信 */
    public static boolean sendSMSAddMenoy(String phoneNo, long amount, long balance){
    	return sendForAmtChange(phoneNo, "收入", amount, balance);
    }
    
    /** 发送减钱短信 */
    public static boolean sendSMSSubMenoy(String phoneNo, long amount, long balance){
    	return sendForAmtChange(phoneNo, "支出", amount, balance);
    }

    /** 发送用于设置支付密码的验证码 */
	public static JSONObject sendForSetPayPwd(String phoneNo, String code) {
		JSONObject param = new JSONObject();
		param.put("code", code);
		ZOAliSms sms = new ZOAliSms(SystemConfig.config.get("sms_accessKeyId"), SystemConfig.config.get("sms_accessSecret"));
		return sms.send(phoneNo, param.toJSONString(), SMS_Name, Tpl_SetPayPassword);
	}
	
	/** 发送用于登录的验证码 */
	public static JSONObject sendForLogin(String phoneNo, String code) {
		JSONObject param = new JSONObject();
		param.put("code", code);
		ZOAliSms sms = new ZOAliSms(SystemConfig.config.get("sms_accessKeyId"), SystemConfig.config.get("sms_accessSecret"));
		return sms.send(phoneNo, param.toJSONString(), SMS_Name, Tpl_Login);
	}
	
	/** 发送用于钱包支出的验证码 */
	public static JSONObject sendForExpend(String phoneNo, String code, long amount) {
		JSONObject param = new JSONObject();
		param.put("code", code);
		param.put("phone", SMS_Service);
		param.put("todoamtShow", ":￥" + new DecimalFormat("#,##0.00").format(amount / 100.0));
		ZOAliSms sms = new ZOAliSms(SystemConfig.config.get("sms_accessKeyId"), SystemConfig.config.get("sms_accessSecret"));
		return sms.send(phoneNo, param.toJSONString(), SMS_Name, Tpl_Expend);
	}
	
}
